#!/bin/bash
. /opt/hygon/common.sh

os_release=$(eval get_os_release)
echo $os_release
case $os_release in
     "anolis" | "centos" | "kylin")
		cp /boot/vmlinuz-$(uname -r) ./vmlinuz
		cp /boot/initramfs-$(uname -r).img ./initramfs.img
		mv initramfs.img initramfs.img.gz
		chmod 777 initramfs.img.gz
		gunzip initramfs.img.gz
		mkdir initramfs
		cd initramfs
		cpio -idm < ../initramfs.img
		cp /opt/hygon/csv/attestation/get_key ./
		cp -f /usr/sbin/dhclient-script usr/sbin
		cp -f /usr/bin/ipcalc usr/bin
		cp -f /usr/bin/cut    usr/bin
		cp -f /usr/sbin/arping  usr/sbin
		cp -f /usr/bin/awk   usr/bin
		cp -f /usr/bin/uniq  usr/bin/uniq
		cp -f /usr/bin/logger  usr/bin/
		cp -f /usr/bin/mktemp   usr/bin/
		mkdir -p var/lib/dhclient
		touch var/lib/dhclient/dhclient.leases
		find . |sudo cpio -o -H newc |sudo gzip > ../initramfs.img
             ;;
     "ubuntu")
		cp /boot/vmlinuz-$(uname -r) ./vmlinuz
		cp /boot/initrd.img-$(uname -r) ./initramfs.img
		mkdir initramfs
		unmkinitramfs initramfs.img initramfs
		cp /opt/hygon/csv/attestation/get_key initramfs/main
		cd initramfs/main
		find . | cpio --create --format=newc | xz --format=lzma > ../main.img
		cd ../early
		find . -print0 | cpio --null --create --format=newc > ../early.img
		cd ../early2/
		find . -print0 | cpio --null --create --format=newc > ../early2.img
		cd ../
		cat early.img early2.img main.img > ../initramfs.img
             ;;
     esac

#calculated measure
cd /opt/hygon/csv/
build_id=`sudo hag csv platform_status  |grep "build id"`
build_id=`echo ${build_id##*:}`
hag csv export_cert_chain
hag csv generate_policy  --nodebug
hag csv generate_launch_blob -build $build_id -bios OVMF.fd -kernel vmlinuz  -initrd initramfs.img
